<?php
declare (strict_types=1);

namespace app\command\company;

use app\admin\model\Company;
use app\company\model\Store;
use app\company\model\StoreSupplier;
use app\company\model\Supplier;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class AddStoreSupplier extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:add_store_supplier')
             ->addArgument('company_id', Argument::REQUIRED, '公司ID')
             ->addArgument('supplier_id', Argument::REQUIRED, '供应商ID')
             ->setDescription('临时脚本，执行即删除');
    }

    protected function execute(Input $input, Output $output)
    {
        $companyId  = $input->getArgument('company_id');
        $supplierId = $input->getArgument('supplier_id');

        if (!Company::find($companyId)) {
            $output->writeln('公司不存在');
            return;
        }
        if (!Supplier::find($supplierId)) {
            $output->writeln('供应商不存在');
            return;
        }
        $output->writeln($companyId,$supplierId);
        return;
        // 今日导入的门店id
        $storeIds = Store::where('company_id', $companyId)
                         ->where('status', 1)
                         ->column('id');

        $model = new StoreSupplier();
        $model->startTrans();
        try {
            foreach ($storeIds as $storeId) {
                // 查询所属门店
                $row = $model->where('company_id', $companyId)
                                    ->where('supplier_id', $supplierId)
                                    ->where('store_id', $storeId)
                                    ->find();
                if (!$row) {
                    $model->create([
                                              'company_id'  => $companyId,
                                              'supplier_id' => $supplierId,
                                              'store_id'    => $storeId
                                          ]);
                }
            }
            $model->commit();
        }catch (\Throwable $e){
            $model->rollback();
            $output->writeln('执行失败:'.$e->getMessage());
        }
        $output->writeln('执行成功');
    }
}
